<!--  -->
<template>
  <div class="login">
    <div class="top"></div>
    <div class="wrap">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
      >
        <!-- 账号输入 -->
        <el-form-item label="账号" prop="name">
          <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>

        <!-- 密码输入 -->
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>

        <!-- 按钮 -->
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="bottom"></div>
  </div>
</template>

<script>
import { getLogin } from "@/util/home.js";
export default {
  name: "",
  data() {
    return {
      ruleForm: {
        phone: "",
        password: "",
      },
      rules: {
        phone: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 3,
            max: 18,
            message: "长度在 3 到 18 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 18,
            message: "长度在 3 到 18 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    async submitForm() {
      let { phone, password } = this.ruleForm;
      let { data } = await getLogin(phone, password);
      console.log(data);
      if (data.code !== 200) {
        return this.$message.error("登陆失败,请检查登录信息!");
      } else {
        localStorage.setItem("cookie", data.cookie);
        this.$message.success("登陆成功");
        this.$router.push("/home");
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped lang='less'>
.login {
  display: flex;
  flex-direction: column;
}
.wrap {
  width: 50vw;
  height: 34vh;
  margin: 0 auto;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
}
.top {
  width: 100vw;
  height: 33vh;
  background-color: #003674;
}
.bottom {
  width: 100vw;
  height: 33vh;
  background-color: #003674;
}
.el-input {
  width: 300px;
}
</style>